pub trait VecExtend<T>{
    fn find<F>(&self,f:F)->Option<&T> where F:Fn(&T)->bool;
    fn remove_where<F>(&mut self,f:F)-> bool where F:Fn(&T)->bool;
    fn find_where<F>(&self,f:F)->Vec<&T> where F:Fn(&T)->bool;
}

impl<T> VecExtend<T> for Vec<T>{
    fn find<F:Fn(&T)->bool>(&self,f:F)->Option<&T> {
        for x in self.iter(){
            if f(x)
            {
                return Some(x);
            }
        }
        None
    }
    fn remove_where<F: Fn(&T) -> bool>(&mut self, f: F)-> bool {
        let mut i = self.len();
        while i > 0 {
            i -= 1;
            if f(&self[i]) {
                self.remove(i);
                return true;
            }
        }
        return false;
    }
    fn find_where<F: Fn(&T) -> bool>(&self, f: F)-> Vec<&T>{
        let mut new_vec:Vec<&T>= Vec::new();
        for x in self.iter(){
            if f(x)
            {
                new_vec.push(x)
            }
        }
        return new_vec;
    }
    
}

use std::{future::Future, sync::Arc};
use tokio::sync::Mutex;

pub async fn find_async<T, F, Fut>(vec: &[Arc<Mutex<T>>], f: F) -> Option<Arc<Mutex<T>>>
where
    Fut: Future<Output = bool>,
    F: Fn(Arc<Mutex<T>>) -> Fut,
{
    for x in vec.iter() {
        let x_clone = Arc::clone(x);
        if f(x_clone).await {
            return Some(Arc::clone(x));
        }
    }
    None
}

pub async fn remove_where_async<T, F, Fut>(vec: &mut Vec<Arc<Mutex<T>>>, f: F)->bool
where
    Fut: Future<Output = bool>,
    F: Fn(Arc<Mutex<T>>) -> Fut
    {
           let mut i = vec.len();
    while i > 0 {
        i -= 1;
        let x_clone = Arc::clone(&vec[i]);
        if f(x_clone).await {
            vec.remove(i);
            return true;
        }
    }
    return false;
}


//use std::future::Future;

// pub trait VecExtend<T>{
//     fn find_async<'a, Fut,F>(&'a self,f:F)-> Option<&'a T> where Fut:Future<Output = bool>, F:Fn(&T)->Fut, T: 'a;
// }

// impl<T> VecExtend<T> for Vec<T>{
//     fn find_async<'a, Fut,F>(&'a self,f:F)-> Option<&'a T> where Fut:Future<Output = bool>, F:Fn(&T)->Fut, T: 'a {
//     for x in self.iter(){
//     if f(x).await
//     {
//     return Some(x);
//     }
//     }
//         None
//     }
// }